package view.swing.popup;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Letter;
import model.Tile;
import util.Resources;

public class SwapChooser {

    private List<Tile> tiles;
    private Component parentWindow;

    public SwapChooser(Component parentWindow, List<Tile> tiles) {
        this.tiles = tiles;
        this.parentWindow = parentWindow;
    }

    public List<Tile> getChoice() {

        ArrayList<Tile> returnTiles = new ArrayList<Tile>();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JLabel lbl = new JLabel(Resources.getString("Game.SwapChoice"));
        lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(lbl);
        panel.add(Box.createVerticalGlue());
        HashMap<Tile, JCheckBox> checkboxes = new HashMap<Tile, JCheckBox>();
        for (Tile t : tiles) {
            JCheckBox jcb;
            String label = "";
            if (t.getLetter() != Letter.BLANK) {
                
                label = Resources.getString("Bag.Letter")+" "+t.getLetter();
            } else {
                label = Resources.getString("Bag.Joker")+" "+t.getLetter();
            }
            label += " ("+t.getScore()+")";
            jcb = new JCheckBox(label);
            checkboxes.put(t, jcb);
            jcb.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(jcb);
        }

        int retour = JOptionPane.showConfirmDialog(parentWindow, panel,
                Resources.getString("Game.SwapTiles"), JOptionPane.OK_CANCEL_OPTION);
        if (retour == JOptionPane.CANCEL_OPTION)
            return null;
        for (Tile t : checkboxes.keySet()) {
            if (checkboxes.get(t).isSelected()) {
                returnTiles.add(t);
            }
        }

        return returnTiles;
    }
}
